package org.publictransport.client.gwt.client.entrypoints;

import java.util.List;

import org.publictransport.client.gwt.client.data.TransportLineSerializable;
import org.publictransport.client.gwt.client.stubs.TransportDataGWTService;
import org.publictransport.client.gwt.client.stubs.TransportDataGWTServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class TransportWebClientEntrypoint implements EntryPoint {
	private TransportDataGWTServiceAsync dataService;
	private MapWidget map;
	@Override
	public void onModuleLoad() {
		dataService = GWT.create(TransportDataGWTService.class);
		ServiceDefTarget serviceDefTarget = (ServiceDefTarget) dataService;
		serviceDefTarget.setServiceEntryPoint("/transportService");
		Button saveButton = new Button("Save button");
		saveButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				saveData();
			}
		});
		Button loadButton = new Button("Load button");
		loadButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				loadData();
				
			}
		});
		
		RootPanel buttonPanel = RootPanel.get("button-div");
		buttonPanel.add(saveButton);
		buttonPanel.add(loadButton);
		
		initMap();
		

	}
	
	private void initMap() {
		RootPanel mapPanel = RootPanel.get("map");
		map = new MapWidget();
		map.setCenter(LatLng.newInstance(47.51186742854768, 19.042654037475586), 15);
		map.addControl(new LargeMapControl3D());
		map.setSize("800px", "600px");
		map.setCurrentMapType(MapType.getHybridMap());
		map.getInfoWindow().open(map.getCenter(), new InfoWindowContent("World's Largest Balls of Steel"));
		mapPanel.add(map);
	}
	
	private void saveData() {
		dataService.saveTransportData(new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
								
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
				
			}
		});	
	}
	
	private void loadData() {
		dataService.loadTransportData(new AsyncCallback<List<TransportLineSerializable>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<TransportLineSerializable> result) {
				for (TransportLineSerializable line : result) {
					RootPanel.get("result").add(new HTML(line.toString()));
				}
			}
		});
	}
}
